package com.sakura.util;/**
 * Created by apple on 2019/3/25.
 */

import com.sakura.conf.DBManager;
import com.sakura.conf.TableContext;
import com.sakura.convert.TypeConvert;
import com.sakura.convert.impl.TypeConvertImpl;
import com.sakura.model.ColumnInfo;
import com.sakura.model.JavaFieldGetSet;
import com.sakura.model.TableInfo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jixd
 * @Description 自动化生成model类
 * @Date 2019/3/25 4:21 PM
 * @Version 1.0
 **/
public class JavaFileUtils {

    public static JavaFieldGetSet createFieldGetSetSRC(ColumnInfo columnInfo, TypeConvert convert){
        JavaFieldGetSet javaFieldGetSet = new JavaFieldGetSet();
        String javaType = convert.databaseType2Java(columnInfo.getDataType());
        String smallColumnName = StringUtils.underlineToSmallCamel(columnInfo.getName());
        String bigColumnName = StringUtils.underlineToBigCamel(columnInfo.getName());

        //set class field
        javaFieldGetSet.setFieldInfo(new StringBuilder("\tprivate ").append(javaType).append(" ").append(smallColumnName).append(";\n").toString());
        //set get method
        StringBuilder getInfoSb = new StringBuilder();
        getInfoSb.append("\tpublic ").append(javaType)
                .append(" get").append(StringUtils.underlineToBigCamel(bigColumnName)).append("(){\n").toString();
        getInfoSb.append("\t\treturn ").append(smallColumnName).append(";\n");
        getInfoSb.append("\t}\n");
        javaFieldGetSet.setGetInfo(getInfoSb.toString());
        //set set method
        StringBuilder setInfoSb = new StringBuilder();
        setInfoSb.append("\tpublic ").append("void")
                .append(" set").append(StringUtils.underlineToBigCamel(bigColumnName)).append("(){\n").toString();
        setInfoSb.append("\t\tthis.").append(smallColumnName).append(" = ").append(smallColumnName).append(";\n");
        setInfoSb.append("\t}\n");
        javaFieldGetSet.setSetInfo(setInfoSb.toString());

        return javaFieldGetSet;
    }

    public static String createJavaModel(TableInfo tableInfo, TypeConvert typeConvert){
        List<JavaFieldGetSet> srcList = new ArrayList<>();

        String tableName = tableInfo.getTableName();
        Map<String, ColumnInfo> columnMap = tableInfo.getColumns();

        for(ColumnInfo columnInfo : columnMap.values()){
            srcList.add(createFieldGetSetSRC(columnInfo, typeConvert));
        }

        StringBuilder sb = new StringBuilder();
        //package
        sb.append("package "+ DBManager.getConfiguration().getPoPackage()+";\n\n");
        sb.append("public class ");
        sb.append(StringUtils.underlineToBigCamel(tableName));
        sb.append(" {\n");

        for(JavaFieldGetSet javaFieldGetSet : srcList){
            //field
            sb.append(javaFieldGetSet.getFieldInfo());
        }

        for(JavaFieldGetSet javaFieldGetSet : srcList){
            //get method
            sb.append(javaFieldGetSet.getGetInfo());
        }

        for(JavaFieldGetSet javaFieldGetSet : srcList){
            //set method
            sb.append(javaFieldGetSet.getSetInfo());
        }

        sb.append("}");

        return sb.toString();
    }

    public static void createJavaFile(TableInfo tableInfo, TypeConvert typeConvert){
        String srcContent = createJavaModel(tableInfo, typeConvert);
        String srcPath = DBManager.getConfiguration().getSrcPath();

        String packagePath=DBManager.getConfiguration().getPoPackage().replace(".","/");
        System.out.println("packagePath:"+packagePath);
        File f=new File(srcPath+packagePath);

        if(!f.exists()){
            f.mkdirs();
        }

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(f.getAbsoluteFile()+"/"+StringUtils.underlineToBigCamel(tableInfo.getTableName())+".java"));
            System.out.println("FileWriter:"+f.getAbsoluteFile()+"/"+StringUtils.underlineToBigCamel(tableInfo.getTableName())+".java");
            bw.write(srcContent);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void updateJavaPOFile(){
        Map<String,TableInfo>tables= TableContext.tables;
        for(TableInfo tableInfo:tables.values()){
            createJavaFile(tableInfo,new TypeConvertImpl());
        }
    }
}


















